package com.example.bookkeepingelf.base.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.util.Log;

import androidx.core.content.ContextCompat;

import com.example.bookkeepingelf.R;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class ResourceUtils {

    private ResourceUtils(){}

    /**
     * 根据文件名获取图片资源id
     * @param context 上下文对象
     * @param iconName  图片资源名
     * @return
     */
    public static int getIconResourceId(Context context, String iconName) {
        int iconResourceId = context.getResources().getIdentifier(iconName, "drawable", context.getPackageName());
        return iconResourceId;
    }

    /**
     * 根据Drawable对象获取资源名
     * @param context
     * @param drawable
     * @return
     */
    public static String getResourceNameByDrawable(Context context, Drawable drawable) {
        if (drawable instanceof BitmapDrawable) {
            Resources resources = context.getResources();
            for (int id : getAllResourceIds()) {
                Drawable currentDrawable = ContextCompat.getDrawable(context, id);
                if (currentDrawable instanceof BitmapDrawable) {
                    if (((BitmapDrawable) currentDrawable).getBitmap().sameAs(((BitmapDrawable) drawable).getBitmap())) {
                        return resources.getResourceEntryName(id);
                    }
                }
            }
        }
        if (drawable instanceof VectorDrawable) {
            Resources resources = context.getResources();
            for (int id : getAllResourceIds()) {
                Drawable currentDrawable = ContextCompat.getDrawable(context, id);
                if (currentDrawable instanceof VectorDrawable) {
                    // 由于 VectorDrawable 不能像 BitmapDrawable 那样比较内容，这里比较资源 ID
                    if (id == getDrawableResourceId(context, drawable)) {
                        return resources.getResourceEntryName(id);
                    }
                }
            }
        }
        return "未知图片";
    }


    /**
     * 根据Drawable对象获取资源id
     * @param context
     * @param drawable
     * @return
     */
    private static int getDrawableResourceId(Context context, Drawable drawable) {
        Resources resources = context.getResources();
        String packageName = context.getPackageName();
        int id = resources.getIdentifier(
                resources.getResourceEntryName(getDrawableResIdByDrawable(context, drawable)),
                "drawable",
                packageName
        );
        return id;
    }

    /**
     * 根据Drawable对象获取资源id
     * @param context
     * @param drawable
     * @return
     */
    private static int getDrawableResIdByDrawable(Context context, Drawable drawable) {
        for (int id : getAllResourceIds()) {
            Drawable currentDrawable = ContextCompat.getDrawable(context, id);
            if (currentDrawable != null && currentDrawable.getClass().equals(drawable.getClass())) {
                return id;
            }
        }
        return 0;
    }

    /**
     * 获取所有drawable资源id
     * @return
     */
    private static List<Integer> getAllResourceIds() {
        Field[] fields = R.drawable.class.getFields();
        List<Integer> ids = new ArrayList<>();
        for (Field field : fields) {
            try {
                ids.add(field.getInt(null));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return ids;
    }


    /**
     * 根据前缀获取所有drawable资源名
     * @param prefix
     * @param prefix
     * @return
     */
    public static List<String> getDrawableNamesStartingWith(String prefix) {
        List<String> drawableNames = new ArrayList<>();
        Field[] fields = R.drawable.class.getFields();
        for (Field field : fields) {
            try {
                String name = field.getName();
                if (name.startsWith(prefix)) {
                    drawableNames.add(name);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return drawableNames;
    }

}
